package com.ttdev.mmms.controller.register;


import com.ttdev.mmms.entity.StoreInfo;
import com.ttdev.mmms.service.StoreService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/register")
public class StoreRegisterController {

    @Autowired
    private StoreService storeService;

    @RequestMapping("/index")
    public String index(){
        return "storeRegister/storeRegister";
    }

    // 查询用户名是否存在
    @RequestMapping("/selectStoreName")
    public void selectStoreName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String msg;
        String storeName = request.getParameter("storeName");
//        System.out.println(storeName);
        Integer i = storeService.selectStoreName(storeName);
        if (i == null){
            i = 0;
        }
        // 如果不为空则商店名已存在
        if (storeName.equals("")){
            msg = "请输入商店名";
        } else if (i == 1 || i > 1){
            msg = "商店名已存在";
        } else {
            msg = "可以使用此商店名";
        }

        JSONObject json = new JSONObject();
        json.put("msg",msg);
//            System.out.println(i);
        // json乱码解决
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(json.toString());
    }

    // 查询用户名是否存在
    @RequestMapping("/selectUser")
    public void select(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uname;
        String name = request.getParameter("name");
//        System.out.println(name);
        Integer i = storeService.selectName(name);
        if (i == null){
            i = 0;
        }
        // 如果不为空则用户名已存在
        if (name.equals("")){
            uname = "请输入用户名";
        } else if (i == 1 || i > 1){
            uname = "用户名已存在";
        } else {
            uname = "可以使用此用户名";
        }

        JSONObject json = new JSONObject();
        json.put("uname",uname);
//            System.out.println(i);
        // json乱码解决
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(json.toString());

    }

    // 添加商家
    @RequestMapping("/addStore")
    public ModelAndView add(StoreInfo storeInfo, HttpServletResponse response) throws IOException {
        int i = storeService.addStore(storeInfo);
//        System.out.println(i);

        ModelAndView modelAndView = new ModelAndView();
        response.setContentType("text/html; charset=UTF-8"); //转码
        PrintWriter out = response.getWriter();

        // 注册失败
        if(i == 0){
            out.flush();
            out.println("<script>");
            out.println("alert('注册失败，请重新注册！');");
            out.println("history.back();");
            out.println("</script>");
            modelAndView.setViewName("forward:/register/index");
            return modelAndView;
        }

        // 注册成功
        out.flush();
        out.println("<script>");
        out.println("alert('注册成功！');");
        out.println("history.back();");
        out.println("</script>");

        modelAndView.setViewName("forward:/");
        return modelAndView;
    }


}
